***********************
*Time Series Analysis for the Social Sciences
*Box-Steffensmeier, Freeman, Hitt, and Pevehouse
*
*Chapter 3 replication code
************************

************************
*Figure 3.1
*Use DurrGilmourWolbrecht_cong_econ.dta
*Source: Durr, Gilmour, and Wolbrecht 1997
*************************

line capp qdate, yaxis(1) yscale(range(50 80) axis(1)) /// 
|| line econexp qdate, scheme(s1mono) yaxis(2) yscale(range(50 100) axis(2)) ///
title(U.S. Congressional Approval and Economic Expectations)

*************************
*Figure 3.2, Table 3.1, Table 3.2, Appendix
*Use QJPS113.dta
*Source: Sattler, Brandt, and Freeman 2010
*************************

tsset time

*Figure 3.2

tsline vi, title(U.K. Voter Intentions) 
tsline usxr, title(Pound-Dollar Exchange Rate)
tsline ir, title(Short-term Interest Rate)


*Table 3.1

	*OLS

reg vi vilag usxr 

	*Alt DW (repeat post-estimations commands for all models)
	
estat durbinalt

	*BG(1)
	
estat bgodfrey, lags(1)

	*Step 2
	*Setup 
reg vilag usxr xrlag
estat durbinalt
estat bgodfrey

predict pvilagxr

	*Step 2 estimates
	
reg vi pvilagxr usxr

	*Step 4 Pseudo-IV GLS(se) AR(1), Column 4, Table 3.1
		
		*Setup for Pseudo-IV step, see Appendix to Chapter 3
	
scalar define b1 = _b[pvilagxr]
scalar define b2 = _b[usxr]

constraint 1 _b[vilag] = b1
constraint 2 _b[usxr] = b2

cnsreg vi vilag usxr, c(1-2)

predict rxr1, resid

ac rxr1, name(acfxr)
pac rxr1, name(pacfxr)

arima rxr1, arima(1,0,0) noconstant
predict ar1, resid
corrgram ar1, lags(12)
arima rxr, arima(2,0,0) noconstant
predict ar2, resid
corrgram ar2, lags(12)
arima rxr, arima(3,0,0) noconstant
predict ar3, resid
corrgram ar3, lags(12)

ac rxr1, gen(rhoxr) lags(1) nograph
tab rhoxr
scalar rhoxr = rhoxr
drop rhoxr
scalar list rhoxr
corrgram rxr1, lags(12)

gen gvixr1 = vi[_n]-rhoxr*vi[_n-1]
gen gxrvi1 = vilag[_n]-rhoxr*vilag[_n-1]
gen gxr1 = usxr[_n]-rhoxr*usxr[_n-1]

	*Final estimates

reg gvixr1 gxrvi1 gxr1

	*Step 4 Pseudo-IV GLS(se) AR(2) estimation (Column 5, Table 3.1)

		*Setup for Pseudo-IV step, see Appendix to Chapter 3

scalar define b1 = _b[pvilagxr]
scalar define b2 = _b[usxr]

constraint 1 _b[vilag] = b1
constraint 2 _b[usxr] = b2

cnsreg vi vilag usxr, c(1-2)

predict rxr, resid

arima rxr, arima(1,0,0) noconstant
predict ar1, resid
corrgram ar1, lags(12)
arima rxr, arima(2,0,0) noconstant
predict ar2, resid
corrgram ar2, lags(12)
arima rxr, arima(3,0,0) noconstant
predict ar3, resid
corrgram ar3, lags(12)
 
scalar rho1 = .8344677
scalar rho2 = .0689635
scalar list

gen gvixr = vi[_n]-rho1*vi[_n-1]-rho2*vi[_n-2]
gen gxrvi = vilag[_n]-rho1*vilag[_n-1]-rho2*vilag[_n-2]
gen gxr = usxr[_n]-rho1*usxr[_n-1]-rho2*usxr[_n-2]

	*Final estimates

reg gvixr gxrvi gxr

**********************

*Table 3.2

	*OLS

reg vi vilag ir

	*Diagnostics (perform after each model)
	
estat durbinalt
estat bgodfrey, lags(1)


	*Step 2

		*Setup
		
reg vilag ir irlag

predict pvilagir

		*Estimates
		
reg vi pvilagir ir

	*Step 4
	
		*Setup, see Appendix to Chapter 3

scalar define b1 = _b[pvilagir]
scalar define b2 = _b[ir]

constraint 1 _b[vilag] = b1
constraint 2 _b[ir] = b2

cnsreg vi vilag ir, c(1-2)

predict rir, resid

arima rir, arima(1,0,0) noconstant
predict ar1ir, resid

ac rir, gen(rhoir) lags(1) nograph
tab rhoir
scalar rhoir = rhoir
drop rhoir
scalar list rhoir
corrgram rir, lags(12)

gen gviir = vi[_n]-rhoir*vi[_n-1]
gen girvi = vilag[_n]-rhoir*vilag[_n-1]
gen gir = ir[_n]-rhoir*ir[_n-1]

	*Final estimates 

reg gviir girvi gir

***********************

*Appendix

*Figure 3.3

ac rxr
pac rxr

graph combine ac_rxr.gph pac_rxr.gph, col(2)

*Figure 3.4

ac rir
pac rir

graph combine ac_rir.gph pac_rir.gph, col(2)
